﻿CREATE PROCEDURE [maint].[SetErrorStatus]
@DbName [sysname], @Action CHAR (1), @ErrorStatus BIT
AS
BEGIN
	DECLARE @RC INT;
	SET @RC = 0;
	
	DECLARE @ErrorTypeBit INT;
	SET @ErrorTypeBit = CASE @Action
			WHEN 'I' THEN 1
			WHEN 'S' THEN 2
			WHEN 'C' THEN 4
		END;
		
	UPDATE [config].[DbMaintenance]
	SET
		[ErrorStatus] = CASE @ErrorStatus
			-- When there's an error, set that bit regardless of its current setting.
			WHEN 1 THEN [ErrorStatus] | @ErrorTypeBit
			-- When there is no error, unset that bit regardless of its current setting.
			ELSE [ErrorStatus] & (~ @ErrorTypeBit)
		END
	WHERE ([DbName] = @DbName);
		
	RETURN(@RC);
END